package com.example.mediarecorderdemo;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.example.mediarecorderdemo.databinding.FragmentPlayRecordBinding;
import java.io.File;
import java.io.IOException;

public class PlayRecordFragment extends Fragment {
    private FragmentPlayRecordBinding mPlayRecordBinding;
    private MediaPlayer mMediaPlayer;

    public PlayRecordFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mPlayRecordBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_play_record, container, false);
        this.mPlayRecordBinding.setLifecycleOwner(this.requireActivity());
        return this.mPlayRecordBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        File file = new File(requireActivity().getExternalFilesDir(""), "1.mp4");
        if (file.exists()) {
            Log.d("TAG", "onActivityCreated: true" + file.getAbsolutePath());
        } else {
            Log.d("TAG", "onActivityCreated: false");
        }

        this.mPlayRecordBinding.btnJump.setOnClickListener((view) -> {
            NavController navController = Navigation.findNavController(view);
            navController.navigate(R.id.action_playRecordFragment_to_playAudioFragment2);
        });
        this.mPlayRecordBinding.btnOpt.setOnClickListener((view) -> {
            CharSequence btnOptText = this.mPlayRecordBinding.btnOpt.getText();
            if (TextUtils.equals(btnOptText, "开始")) {
                this.mPlayRecordBinding.btnOpt.setText("结束");
                this.mMediaPlayer = new MediaPlayer();
                this.mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        PlayRecordFragment.this.mMediaPlayer.start();
                    }
                });
                this.mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        PlayRecordFragment.this.mPlayRecordBinding.btnOpt.setText("开始");
                        PlayRecordFragment.this.mMediaPlayer.stop();
                        PlayRecordFragment.this.mMediaPlayer.release();
                    }
                });

                try {
                    this.mMediaPlayer.setDataSource(file.getAbsolutePath());
                    this.mMediaPlayer.setSurface(this.mPlayRecordBinding.textureView.getHolder().getSurface());
                    this.mMediaPlayer.prepareAsync();
                } catch (IOException var5) {
                    var5.printStackTrace();
                }
            } else {
                this.mPlayRecordBinding.btnOpt.setText("开始");
                this.mMediaPlayer.stop();
                this.mMediaPlayer.release();
            }

        });
    }
}
